/* pollsrv.c */
#include <stdio.h>
#include <sys/types.h> 
#include <sys/socket.h> 
#include <sys/un.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <fcntl.h>
#include <fstream>
#include <signal.h>
#include <sys/stat.h>
#include <string>
#include <iostream>
#include <dirent.h>
#include <errno.h>
#include <sys/param.h>
#include <fcntl.h>
#include <netinet/tcp.h>
#include <sstream>
#include <time.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>

using namespace std;

int main(int argc, char *argv[])
{

	struct hostent *host = gethostbyname("localhost") ;
	struct sockaddr_in in_addr;
	if(host != NULL)
	{
		in_addr.sin_addr.s_addr = *((u_long *)(host->h_addr));
		cout << inet_ntoa(in_addr.sin_addr) <<endl;
	}
        else
             return -1;
      int sockfd = 0;
      if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1)
      {
         perror("socket");
	 exit(1);
      }
      their_addr.sin_family = AF_INET;
      their_addr.sin_port = htons( atoi(argv[2]));
      their_addr.sin_addr = *((struct in_addr *)he->h_addr);
      bzero(&(their_addr.sin_zero),8);
      if(connect(sockfd,(struct sockaddr *)&their_addr, sizeof(struct sockaddr)) ==-1){
	      perror("connect");
	      exit(1);
      }
   
      char sbuf[128];
      int slen =  sprintf(sbuf,"send");
      sbuf[slen] = '\0';
      slen = send(sockfd, sbuf, dlen, 0);
      char rbuf[2048];
      int readlen = 0;
      int m_iMTU_recv = 1500;
      while (true) {
	      int res = recv(sockfd, rbuf, m_iMTU_recv, 0);
	      if (res > 0) {
		      readlen += res;
		      if (res ==  m_iMTU_recv) {
			      continue;
		      } else {
			      //DEBUG4("avail = " , avail ," res = ", res);
			      break;
		      }
	      }
	      if (res == 0) {
	//	      DEBUG2("readlen = ", readlen);
		      break;
	      }
	      if(res < 0)
	      {    break; }
      }
     
      cout<<"readlen = "<< readlen <<endl;
      fclose(sockfd);
      return 0;

}

